package com.xinxin.blockchain.common.aspect;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xinxin.base.entity.ApiResult;
import com.xinxin.base.exception.BusinessException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class InterfaceAspect {
    private static final Logger log = LoggerFactory.getLogger(InterfaceAspect.class);

    @Pointcut("execution(public * com.xinxin.blockchain.*.controller.*.*(..)) && " +
            "!execution(* com.xinxin.blockchain.*.controller.*.getCurrentUser())")
    private void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        try {
            String className = point.getTarget().getClass().getSimpleName();
            String methodName = point.getSignature() == null ? "" : point.getSignature().getName();

            Object result = point.proceed();
            log.info("\n{}——{}.{} 接口返回：\n{}", Thread.currentThread(), className, methodName,
                    JSONObject.toJSONString(result, SerializerFeature.PrettyFormat));

            return result;
        } catch (BusinessException e) {
            log.error("系统错误", e);
            return ApiResult.code(e.getCode()).msg(e.getMessage());
        }
    }
}
